namespace musicplayer
{
public partial class Form1 : Form
{
//声明一个list,用来存储文件的路径
List<string> urlList = new List<string>();//泛型
double max, min;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 添加歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInput_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//让选择器可以选择多个文件
of.Multiselect = true;
of.Title = "请选择音乐文件";
//指定选择文件的类型
//方法一
//of.Filter = "音乐文件|*.mp3|WAV文件|所有文件|*.*";
//方法二
of.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
//方法三
//of.Filter = "(*.mp3)|*.mp3";
//确认用户选择的是确认按钮
//of.ShowDialog();
if (of.ShowDialog() == DialogResult.OK)
{
//把用户选择的文件存储到数组中
string[] nameList = of.FileNames;
//读取数组中的数据
foreach (string url in nameList)
{
//Path.GetFileNameWithoutExtension(url)获取具有扩展名的文件名
listBoxMusics.Items.Add(Path.GetFileName(url));
urlList.Add(url);
}
}
}
/// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnplay_Click(object sender, EventArgs e)
{
//声明变量
int selectedIndex = listBoxMusics.SelectedIndex;
if (selectedIndex < 0)
{
//判断列表中是否有选中的歌曲,有的话播放选中的,没有的话,播放第一首
selectedIndex = selectedIndex < 0 ? 0 : selectedIndex;
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
timer1.Enabled = true;
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnpause_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnstop_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
//歌曲停止后禁用进度条
timer1.Enabled = false;
}
/// <summary>
/// 列表选择发生变化时,播放选中歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxMusics_SelectedIndexChanged(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
timer1.Enabled = true;
}
/// <summary>
/// 上一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex - 1;
selectedIndex = selectedIndex < 0 ? 0 : selectedIndex;//三元算符
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
/// <summary>
/// 下一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex 1;
selectedIndex = selectedIndex == listBoxMusics.Items.Count ? listBoxMusics.SelectedIndex : selectedIndex;//三元元算符
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
/// <summary>
/// 进度条控制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//获取文件的长度
max = axWindowsMediaPlayer1.currentMedia.duration;
//获取当前歌曲的播放位置
min = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
//类型强转
trackBar1.Maximum = (int)(max);
trackBar1.Value = (int)(min);
//一首歌播放完成后,继续下一首
if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
{
//当前歌曲播放结束后,要获取下一首歌曲的索引值
int SelectedIndex = listBoxMusics.SelectedIndex 1;
SelectedIndex = SelectedIndex == listBoxMusics.Items.Count ? 0 : SelectedIndex;//三元元算符
axWindowsMediaPlayer1.URL = urlList[SelectedIndex];
listBoxMusics.SelectedIndex = SelectedIndex;
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
trackBar1.Value = 0;
timer1.Enabled = true;
}
}
/// <summary>
/// 鼠标按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_MouseDown(object sender, MouseEventArgs e)
{
//暂停播放
timer1.Enabled = false;
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
/// <summary>
/// 鼠标抬起
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
double dovalue = trackBar1.Value;//获取被拖动以后的位置
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = dovalue;//重置播放位置
axWindowsMediaPlayer1.Ctlcontrols.play();
timer1.Enabled = true;
}
public int SelectedIndex { get; set; }
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
/// <summary>
/// 窗口加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{ //窗体启动最大化
//this.WindowState = FormWindowState.Maximized;
//程序加载更换皮肤
skinEngine1.SkinFile = @"D:\documents\visual studio 2012\Projects\musicplayer\musicplayer\bin\Debug\Debug\skin\DiamondGreen.ssk";
//设置图片在pictureBox1布局,使图片适应大小
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void button1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 图片自动播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
string[] imgspath = Directory.GetFiles(@"C:\Users\Administrator\Desktop\lmages");
i ;
if (i == imgspath.Length)
{
i = 0;
}
pictureBox1.Image = Image.FromFile(imgspath[i]);
}
public int i { get; set; }
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
/// <summary>
/// 删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listBoxMusics.SelectedIndex == -1)
{
return;
}
listBoxMusics.Items.Remove(listBoxMusics.SelectedItem);
}
/// <summary>
/// 清空列表事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
{
//清空之前先提示用户是否确认清空
if(MessageBox.Show("是否清空列表?")==DialogResult.OK)
{
listBoxMusics.Items.Clear();//清空
}
}
/// <summary>
/// 音量控制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar2_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar2.Value;//设置播放音量大小
}
/// <summary>
/// 窗体关闭前提醒
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//MessageBox.Show("窗口关闭时");
DialogResult dr;
dr = MessageBox.Show("确认退出吗", "确认对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Yes)
{ }
else
{
e.Cancel = true;
}
}
private void label1_Click_1(object sender, EventArgs e)
{
}
}
}
评论